package com.zhoukai.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhoukai.dto.DeptDto;
import com.zhoukai.vo.DataGridView;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhoukai.mapper.DeptMapper;
import com.zhoukai.domain.Dept;
import com.zhoukai.service.DeptService;
/**
 * @Description TODO
 * @Author Mr.ZHOU
 * @Date Created in 2022/1/6 10:47
 */

@Service
public class DeptServiceImpl  implements DeptService{
    @Autowired
    private DeptMapper deptMapper;
    @Override
    public DataGridView listDeptForPage(DeptDto deptDto) {

        Page<Dept> page=new Page<>(deptDto.getPageNum(), deptDto.getPageSize());
        QueryWrapper<Dept> qw=new QueryWrapper<>();
        qw.like(StringUtils.isNotBlank(deptDto.getDeptName()), Dept.COL_DEPT_NAME, deptDto.getDeptName());
        qw.eq(StringUtils.isNotBlank(deptDto.getStatus()), Dept.COL_STATUS, deptDto.getStatus());
        qw.ge(deptDto.getBeginTime()!=null, Dept.COL_CREATE_TIME,deptDto.getBeginTime());
        qw.le(deptDto.getEndTime()!=null,Dept.COL_CREATE_TIME,deptDto.getEndTime());
        qw.orderByAsc(Dept.COL_ORDER_NUM);
        this.deptMapper.selectPage(page,qw);
        return new DataGridView(page.getTotal(),page.getRecords());

    }

    @Override
    public int addDept(DeptDto deptDto) {
        Dept dept=new Dept();
        BeanUtil.copyProperties(deptDto,dept);
        //设置创建者，。创建时间
        dept.setCreateBy(deptDto.getSimpleUser().getUserName());
        dept.setCreateTime(DateUtil.date());
        return this.deptMapper.insert(dept);

    }

    @Override
    public Dept getDeptById(Long deptId) {

        return this.deptMapper.selectById(deptId);
    }

    @Override
    public int updateDept(DeptDto deptDto) {
        Dept dept = new Dept();
        BeanUtil.copyProperties(deptDto,dept);
        //设置修改人
        dept.setUpdateBy(deptDto.getSimpleUser().getUserName());

        return this.deptMapper.updateById(dept);
    }

    @Override
    public int deleteDeptByIds(Long[] deptIds) {
        List<Long> ids = Arrays.asList(deptIds);
        if(null!=ids&&ids.size()>0){
            return this.deptMapper.deleteBatchIds(ids);
        }
        return 0;
    }


    @Override
    public List<Dept> selectAllDept() {
        Dept dept=new Dept();
        QueryWrapper<Dept> qw=new QueryWrapper<>();
        return this.deptMapper.selectList(qw);

    }

    }

